import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {Location} from '@angular/common';
import 'rxjs/add/operator/switchMap';
import {HttpClient} from '@angular/common/http';
import {AppUrl} from '../../../app.url';
import {NzMessageService} from 'ng-zorro-antd';
import {HttpError} from '../../../common/http-error';
import {TinymceConfig} from "../../../common/tinymce.config";
import {SettingsService} from "@delon/theme";
import {reject} from "q";

@Component({
    selector: 'app-detail-acti',
    templateUrl: './detail-acti.component.html',
    styleUrls: ['../../sponsor/spon/spon.less', '../../_home/home.less', '../../sponsor/sponInfo/sponInfo.less', './detail-acti.less']
})
export class DetailActiComponent implements OnInit {
    message: string;
    collapseFlag: boolean;
    config = TinymceConfig;
    editorShow = false;
    updating = false;
    private actInfo = {//所显示的活动的详情
        actId: '',
        actBelong: '',
        actType: '',
        actName: '',
        actMoney: 0,
        actProportion: 0,
        actMater: '',
        actBegDate: '',
        actEndDate: '',
        actPlace: '',
        actNum: '',
        actPropaWay: '',
        actFeedback: '',
        number: 1,
        actConfirm: '',
        applicant: []
    };
    private student = { //发起社团信息
        qq: 0,
        sex: '',
        majorSub: '',
        campusId: 0,
        userId: 0,
        special: '',
        universityId: 0,
        areaId: 0,
        positionId: 0,
        phone: '',
        entrance: '',
        department: '',
        username: '',
        authForView: '',
        campusName: ''
    };

    private articleHtml = {//活动中的文章
        articleId: '',
        type: 0,
        uploadTime: '',
        enabled: 0,
        content: ''
    };
    private applyReason = '';
    private applicant = [];//已申请人id
    private applicantCount = 0;//已申请人数

    constructor(private location: Location,
                private route: ActivatedRoute,
                private router: Router,
                private http: HttpClient,
                private nzMessage: NzMessageService,
                private httpError: HttpError,
                public settings: SettingsService) {
        this.message = '输入申请理由';
        this.collapseFlag = false;

    }

    private userActList = [//发布人发布的所有活动列表
        {
            actId: '',
            actName: '',
            actMoney: ''
        }
    ];

    advertise: any[] = [
        {img: './assets/img/coffee1.jpg'},
        {img: './assets/img/coffee2.jpg'},
        {img: './assets/img/coffee3.jpg'},
        {img: './assets/img/coffee4.jpg'}
    ];

    repllyList: any[] = [
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/coffee2.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee3.jpg'}, {avtsrc: './assets/img/coffee4.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/coffee3.jpg'},
        {avtsrc: './assets/img/coffee2.jpg'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/long.png'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee2.jpg'}, {avtsrc: './assets/img/coffee1.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/coffee2.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee3.jpg'}, {avtsrc: './assets/img/coffee4.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/coffee3.jpg'},
        {avtsrc: './assets/img/coffee2.jpg'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/long.png'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee2.jpg'}, {avtsrc: './assets/img/coffee1.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/coffee2.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee3.jpg'}, {avtsrc: './assets/img/coffee4.jpg'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/coffee3.jpg'},
        {avtsrc: './assets/img/coffee2.jpg'}, {avtsrc: './assets/img/coffee1.jpg'}, {avtsrc: './assets/img/long.png'},
        {avtsrc: './assets/img/long.png'}, {avtsrc: './assets/img/coffee2.jpg'}, {avtsrc: './assets/img/coffee1.jpg'}
    ];

    hottor: any[] = [
        '赞助全国高校社团活动',
        '千元赞助，可签合同',
        '以纯A21高校活动赞助',
        '扫码关注公众号并分享',
        '【APP下载推广】全国社团最大APP'
    ];

    showCollapse() {
        this.collapseFlag = !this.collapseFlag;
    }

    ngOnInit(): void {
        let actId = +this.route.snapshot.params.id;
        if (actId == null || actId == undefined) {
            this.nzMessage.create('error', '数据获取失败，请返回重试', {nzDuration: 4000});
        } else {
            this.http.post(AppUrl.activity.selectInfoUrl, {actId: actId}).subscribe(data => {
                    let temp: any = data;
                    if (temp.code == 200) {
                        this.actInfo = temp.data;
                        this.articleHtml = temp.data.article;
                        this.student = temp.data.student;
                        this.applicant = temp.data.applicant;
                        this.applicantCount = this.applicant.length;
                        this.getPage(1);
                    }
                    else if (temp.code == 300)
                        this.nzMessage.error('你要查询的活动不见啦', {nzDuration: 4000});
                }, data => {
                    this.httpError.getError(data);
                }
            );
        }
    }

    //申请活动事件
    applyActivity(): void {
        this.http.post(AppUrl.activity.applyUrl, {
            actId: this.actInfo.actId,
            bId: localStorage.getItem('userid'),
            reason: this.applyReason
        }).subscribe(data => {
                let temp: any = data;
                if (temp.code == 200)
                    this.nzMessage.create('success', '申请成功', {nzDuration: 4000});
                else if (temp.code == 400)
                    this.nzMessage.create('warning', '您已经申请过了', {nzDuration: 4000});
            }, data => this.httpError.getError(data)
        );
    }

    //查看我的申请
    myApplyActivity(): void {
        this.http.post(AppUrl.activity.myApplyUrl, {bId: localStorage.getItem('userid')}).subscribe(data => {
                let temp: any = data;
            }, data => this.httpError.getError(data)
        );
    }

    private isVisible = false;

    showModal = () => {
        this.isVisible = true;
    };

    handleOk = (e) => {
        this.isVisible = false;
    };

    handleCancel = (e) => {
        console.log(e);
        this.isVisible = false;
    };

    private page: any = {
        pageNum: 1,
        pageSize: 3,
        total: 0,
    };

    getPage(pageNum) {
        if (this.actInfo.actBelong != '') {
            this.http.post(AppUrl.activity.selectWhoseListUrl, {
                pageNo: pageNum,
                pageSize: this.page.pageSize,
                sId: this.actInfo.actBelong
            }).subscribe(data => {
                let temp: any = data;
                if (temp.code == 200) {
                    this.page = temp.data;
                    this.userActList = this.page.list;
                }
            }, data => {
                this.httpError.getError(data);
            }, () => {
            });
        }
    }
    //修改
    showEditor() {
        this.editorShow = !this.editorShow;
    }

    //文案编辑人员更新文章
    updateArticle() {
        this.updating = true;
        this.http.post(AppUrl.article.update, {
            articleId: this.articleHtml.articleId,
            content: this.articleHtml.content
        }).subscribe(res => {
            let temp:any = res;
            this.nzMessage.success(temp.msg);
            this.updating = false;
            this.editorShow = false;
        }, reject => {
            this.httpError.getError(reject)
        });
    }

}
